<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-04 11:45:41
 * @LastEditors: Sam
 * @LastEditTime: 2021-12-04 12:34:52
 */
namespace app\__MODULE__\controller;
use app\__MODULE__\model\Banner as BannerModel;

// banner管理模块
class Banner extends Base
{
    // banner位置定义
    private $c = [
        1 => '首页轮播图'
    ];

    // 跳转类型定义
    private $type_list = [
        0 => '不跳转',
        1 => '小程序页面',
        2 => '外链'
    ];

    /**
     * banner列表
     * date 2020/8/13 17:54
     */
    public function index(){
        if(IS_POST) {
            $search  = request()->param();
            $where = [];
            if(isset($search["title"]) && $search["title"]!="") $where[] = ["title","=",$search["title"]];

            $db   = BannerModel::where($where)->order('id desc');
            $list = $db->limit(paginator())->select();
             if(count($list)){
                foreach($list as &$v){
                    $v["position"]  = $this->c[$v['position']];
                    $v["type"]      = $this->type_list[$v['type']];
                    $v['image']     = table_img($v['image']);
                    $v['status']    = table_switch('Banner/is_show', ['id'=>$v['id']], $v['status'] == 1 ? true : false,'是|否');
                }
            }

            admin_response(0, 'success', $list, $db->count());
        }
        return $this->render_table([
            "page"=>true,
            "toolbar" => [table_button('__MODULE__/Banner/add',"添加","frame",'',['850px','600px'])],
            "toolbar_row" => [ 
                    table_button('__MODULE__/Banner/edit',"编辑","frame",'',['850px','600px']),
                    table_button('__MODULE__/Banner/del',"删除","confirm")
                    ],
            "cols" => [ 
                     ["type"=>"checkbox"],
                    ["field"=>"id", "title"=>"ID","align"=>"center"],
                    ["field"=>"position", "title"=>"位置","align"=>"center"],
                    ["field"=>"type", "title"=>"类型","align"=>"center"],
                    ["field"=>"name", "title"=>"标题","align"=>"center"],
                    ["field"=>"image", "title"=>"图片","align"=>"center"],
                    ["field"=>"url", "title"=>"地址","align"=>"center"],
                    ["field"=>"sort", "title"=>"排序","align"=>"center"],
                    ["field"=>"status", "title"=>"是否显示","align"=>"center"]

            ],
            "where"=>[
                    "title"=>["type"=>"input","title"=>"标题"],

            ]

            ],false);

    }

            /**
     * 上架/下架操作
     * date 2021/3/17 11:20
     */
    public function is_show() {
        $id      = param_check('id');
        $checked = param_check('checked', 0);
        $res = BannerModel::where('id', $id)->update([
            'status'        => $checked ? 1 : 0,
            'update_time'   => time()
        ]);
        $res ? json_response(1, '修改成功') : json_response(0, '修改失败');
    }

    /**
     * 添加banner
     * date 2020/8/13 17:54
     */
    public function add() {
        if(request()->isPost()) {
            $data = request()->post();
            if(BannerModel::create($data)) json_response(1,'操作成功');
            json_response(0,'操作失败');
         }else{
             $position = $this->c;
             $jump     = $this->type_list;
             $position_option = [];
             $jump_option     = [];
             foreach ($position as $key => $value) {
                 $position_option[$key]['title'] = $value;
                 $position_option[$key]['value'] = $key;
             }
             foreach ($jump as $key => $value) {
                $jump_option[$key]['title'] = $value;
                $jump_option[$key]['value'] = $key;
            }
            return $this->render_form([
                     ["type"=>"select","name"=>"position","title"=>"图片位置","lay-verify"=>"required",'option'=>$position_option],
                     ["type"=>"text","name"=>"name","title"=>"名称","lay-verify"=>"required"],
                     ["type"=>"upload","name"=>"image","title"=>"图片","lay-verify"=>"required"],
                     ["type"=>"select","name"=>"type","title"=>"跳转类型","lay-verify"=>"required",'option'=>$jump_option],
                     ["type"=>"text","name"=>"url","title"=>"跳转地址","lay-verify"=>"required"],
                     ['type'=>'radio','name'=>'status', 'title'=>'状态','option'=>[
                        ['title'=>'显示','value'=>1],
                        ['title'=>'隐藏','value'=>0]
                    ]],
                    ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
            ]);
         }
    }

    /**
     * 编辑banner
     * date 2020/8/13 17:55
     */
    public function edit() {
        $id = \param_check('id');
        if(request()->isPost()) {
            $data = request()->post();
            if(BannerModel::update($data,['id'=>$id])) json_response(1,'操作成功');
            json_response(0,'操作失败');
         }else{
             $position = $this->c;
             $jump     = $this->type_list;
             $position_option = [];
             $jump_option     = [];
             foreach ($position as $key => $value) {
                 $position_option[$key]['title'] = $value;
                 $position_option[$key]['value'] = $key;
             }
             foreach ($jump as $key => $value) {
                $jump_option[$key]['title'] = $value;
                $jump_option[$key]['value'] = $key;
            }
            $data = BannerModel::find($id);
            return $this->render_form([
                     ["type"=>"select","name"=>"position","title"=>"图片位置","lay-verify"=>"required",'option'=>$position_option],
                     ["type"=>"text","name"=>"name","title"=>"名称","lay-verify"=>"required"],
                     ["type"=>"upload","name"=>"image","title"=>"图片","lay-verify"=>"required"],
                     ["type"=>"select","name"=>"type","title"=>"跳转类型","lay-verify"=>"required",'option'=>$jump_option],
                     ["type"=>"text","name"=>"url","title"=>"跳转地址","lay-verify"=>"required"],
                     ['type'=>'radio','name'=>'status', 'title'=>'状态','option'=>[
                        ['title'=>'显示','value'=>1],
                        ['title'=>'隐藏','value'=>0]
                    ]],
                    ['type'=>'text','text_type'=>'number','name'=>'sort', 'title'=>'排序'],
            ],$data);
         }
    }

    /**
     * 删除banner
     * date 2020/8/13 17:55
     */
    public function del() {
        $id = param_check('id');
        if(!$banner = BannerModel::find($id)) json_response(0,'不存在');
        if($banner->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }

    
}